package com.cch.demo.bus.service.impl;

import com.cch.demo.bus.domain.Goods;
import com.cch.demo.bus.domain.Sales;
import com.cch.demo.bus.domain.Sales;
import com.cch.demo.bus.domain.Sales;
import com.cch.demo.bus.mapper.SalesMapper;
import com.cch.demo.bus.service.GoodsService;
import com.cch.demo.bus.service.SalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 华哥
 * @since 2020-03-12
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService {

    @Resource
    private GoodsService goodsServiceImpl;

    @Override
    public boolean save(Sales entity) {
        //根据商品编号查询商品
        Goods goods = goodsServiceImpl.getById(entity.getGoodsid());
        goods.setNumber(goods.getNumber()-entity.getNumber());
        goodsServiceImpl.updateById(goods);
        //保存销售信息
        return super.save(entity);
    }

    @Override
    public boolean updateById(Sales entity) {
        Sales sales = this.getBaseMapper().selectById(entity.getId());
        Goods goods = goodsServiceImpl.getById(entity.getGoodsid());
        goods.setNumber(goods.getNumber()+sales.getNumber()-entity.getNumber());
        goodsServiceImpl.updateById(goods);
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {

        Sales sales = this.getBaseMapper().selectById(id);
        Goods goods = goodsServiceImpl.getById(sales.getGoodsid());

        goods.setNumber(goods.getNumber()+sales.getNumber());
        goodsServiceImpl.updateById(goods);
        return super.removeById(id);
    }
}
